package org.shop.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.FavoritesService;
import org.shop.service.GoodsService;
import org.shop.service.LeaveMessageService;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UsedController {
	private static Logger logger = LogManager.getLogger(UsedController.class);
	
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private LeaveMessageService leaveMessageService;
	
    /**
     * 跳转二手列表页面
     * @param req
     * @return
     */
    @RequestMapping("app/usedGoodsList")
    public String usedGoodsList(HttpServletRequest req){
    	return "app/usedGoodsList";
    }
    
    /**
     * 获取二手列表（2出售||3求购）
     * @param req
     * @return
     */
    @RequestMapping("app/getUsedList")
    @ResponseBody
    public List<Map<String, Object>> getUsedList(HttpServletRequest req){
    	String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		String goodsIdentifying = req.getParameter("goodsIdentifying");
    	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
        SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
        	ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("schoolId", (long)1);
            params.put("goodsIdentifying", goodsIdentifying);
    		params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
    		params.put("pageSize", pageSize);
            list = goodsService.getUsedGoodsList(params);
            if(list.size() > 0) {
            	for(int i = 0;i < list.size(); i++) {
            		Map<String, Object> m = new HashMap<String,Object>();
            		m = list.get(i);
                    Map<String, Object> params2 = new HashMap<String, Object>();
                    params.put("goodsId", m.get("goodsId"));
            		m.put("leaveMessageCount", leaveMessageService.getGoodsLeaveMessage(params2).size());
            		//查询我是否收藏了此商品
            		if(ObjUtil.isNotNullAndEmpty(ud)) {
            			params.put("userId", ud.getUserId());
            			params.put("targetId", m.get("goodsId"));
            			params.put("attentionType", (byte)1);
            			List<Map<String, Object>> favoritesdList = favoritesService.getFavoritesList(params2);
            			if(favoritesdList.size() > 0) {
            				m.put("isFavoritesd", 1);
            			}else {
            				m.put("isFavoritesd", 0);
            			}
            		}else {
            			m.put("isFavoritesd", 0);
            		}
            		list2.add(m);
            	}
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
        return list2;
    }
    
}
